package com.framework.base.taglib.ui;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.framework.base.cache.BdCache;
import com.framework.base.taglib.ui.common.PowerTag;
import com.framework.base.util.BeanUtil;
import com.framework.base.util.StringUtil;
import com.framework.main.sys.bd.domain.SysBdVO;
import com.framework.main.sys.bd.service.SysBdService;

public class SelectTag extends PowerTag {
	private String id="";
	private String name="";
	private String value="";
	private String style="";
	private String styleClass="";
	private String onclick="";
	private String operation="";//用于控制权限 书定格式为ModuleID.OperationType
	private boolean display=false;//如果没权限时 true代表变灰 false代表不显示
	private String type="button";//按钮类型 默认为按钮
	private List<?> list;//集合数据
	private String listKey;//option的value值
	private String listValue;//option的text值
	
	private String bdTypeId;//id
	
	
	public int doStartTag() throws JspException {
		return super.doStartTag();
	}
	@SuppressWarnings("unchecked")
	public int doEndTag() throws JspException {
		List<SysBdVO> bdlist=null;
		if(BdCache.bdTypeMap.get(bdTypeId)==null){
			SysBdService  bdService=(SysBdService)BeanUtil.getServiceBeanById("bdService");
			try {
				 bdlist=(List<SysBdVO>) bdService.queryForSelectList(bdTypeId);
				 BdCache.bdTypeMap.put(bdTypeId, bdlist);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else{
			bdlist=BdCache.bdTypeMap.get(bdTypeId);
		}
		
			
			if(bdTypeId!=null&&!bdTypeId.equals("")){
				list=bdlist;
				
			}
				JspWriter out = pageContext.getOut();
				ResourceBundle v = ResourceBundle.getBundle("globalMessages", Locale.getDefault());	
				StringBuffer contentsb=new StringBuffer("<select ");
				contentsb.append(StringUtil.isNullorEmpty(getId())?"":"id=\""+getId()+"\" ");
				contentsb.append(StringUtil.isNullorEmpty(getName())?"":"name=\""+getName()+"\" ");
				
				contentsb.append(StringUtil.isNullorEmpty(getStyle())?"":"style=\""+getStyle()+"\" ");
				String id=StringUtil.isNullorEmpty(getId())?"":getId();
				id=id.replace(".", "");
				contentsb.append(StringUtil.isNullorEmpty(id)?"":"class=\"select-"+id+"\" ");
				contentsb.append(StringUtil.isNullorEmpty(getOnclick())?"":"onclick=\""+getOnclick()+"\" ");
				
				boolean canOperation=doJudgePower(getOperation());
				if(canOperation){
					contentsb.append(">");
				}else{
					if(display){
						contentsb.append("disabled=\"disabled\">");
					}
				}
				contentsb.append("<option value=\"\">请选择</option>");
				if(list!=null){
				for (Object obj : list) {
					String getMethodNameKey = "get"+toFirstLetterUpperCase(StringUtil.isNullorEmpty(getListKey())?"":getListKey());
					String getMethodNameValue = "get"+toFirstLetterUpperCase(StringUtil.isNullorEmpty(getListValue())?"":getListValue());
					try {
						Object key = obj.getClass().getMethod(getMethodNameKey).invoke(obj);
						Object value = obj.getClass().getMethod(getMethodNameValue).invoke(obj);
						contentsb.append("<option value=\""+key+"\"");
						if(key.equals(StringUtil.nullToEmpty(getValue()))){
							contentsb.append("selected=\"true\"");
						}
						contentsb.append(">"+value+"</option>");
					} catch (Exception e) {
						e.printStackTrace();
					}   

				}
				}
				contentsb.append("</select>");
				contentsb.append("<script type=\"text/javascript\">$(\".select-"+id+"\").chosen();</script>");
				try {
					out.print(contentsb.toString());
				} catch (IOException e) {
					e.printStackTrace();
				}

		return super.doEndTag();
	}
	public static String toFirstLetterUpperCase(String str) {   
	    if(str == null || str.length() < 2){   
	        return str;   
	    }   
	     String firstLetter = str.substring(0, 1).toUpperCase();   
	     return firstLetter + str.substring(1, str.length());   
	}   

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getStyleClass() {
		return styleClass;
	}
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}
	public String getOnclick() {
		return onclick;
	}
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
	
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public boolean isDisplay() {
		return display;
	}
	public void setDisplay(boolean display) {
		this.display = display;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	public String getListKey() {
		return listKey;
	}
	public void setListKey(String listKey) {
		this.listKey = listKey;
	}
	public String getListValue() {
		return listValue;
	}
	public void setListValue(String listValue) {
		this.listValue = listValue;
	}
	public String getBdTypeId() {
		return bdTypeId;
	}
	public void setBdTypeId(String bdTypeId) {
		this.bdTypeId = bdTypeId;
	}

}
